const app = getApp();
var time = require("../../../../utils/util.js");

Page({

  /**
   * 页面的初始数据
   */
  data: {
    prompt: '',
    //处理方式
    require_array: [{
      id: 1,
      value: '我要退款，但不退货'
    }],
    //退款原因
    reason_array: [{
      id: '买/卖双方协商一致',
      value: '买/卖双方协商一致'
    },
    {
      id: '买错/多买/不想要',
      value: '买错/多买/不想要'
    },
    {
      id: '商品质量问题',
      value: '商品质量问题'
    },
    {
      id: '未收到货品',
      value: '未收到货品'
    },
    {
      id: '其他',
      value: '其他'
    },
    ],
    require_index: 0,
    reason_index: 0,
    refund_money: 0.00, //最多退款金额
    refund_balance: 0.00, //余额退款
    refund_require_money: 0.00, //申请退款金额
    refund_real_money: 0.00, //实际退款金额
    refund_express_company: '', //物流公司
    refund_shipping_no: '', //运单号
    refund_reason: '', //退款理由
    order_id: 0,
    order_goods_id: 0,
    detail: '',
    is_load: 0,
    refundFlag: 0,
    returnsGoodsFlag: 0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this;
    let order_goods_id = options.order_goods_id;

    that.setData({
      order_goods_id: order_goods_id
    })
    that.getRefundInfo();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let that = this;
    app.restStatus(that, 'refundFlag');
    app.restStatus(that, 'returnsGoodsFlag');
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 加载店铺信息
   */
  loadShopInfo: function () {
    let that = this;
    let order_goods_id = that.data.order_goods_id;

    app.sendRequest({
      url: 'System.Shop.shopOrderReturnSet',
      success: function (res) {
        if (res.code == 0) {

          let shop_info = app.checkEmpty(res.data, {});
          shop_info.shop_address = app.checkEmpty(shop_info.shop_address, '');
          that.setData({
            shop_info: shop_info
          })
        }
      }
    });
  },

  /**
   * 获取退款信息
   */
  getRefundInfo: function () {
    let that = this;
    let order_goods_id = that.data.order_goods_id;

    app.sendRequest({
      url: 'System.Order.refundDetail',
      data: {
        order_goods_id: order_goods_id
      },
      success: function (res) {
        if (res.code == 0) {
          let info = app.checkEmpty(res.data, {});
          let detail = app.checkEmpty(info.refund_detail, '');
          console.log(detail);
          if (detail == '') {
            wx.navigateBack({
              delta: 1
            })
            return false;
          }
          that.loadShopInfo();
          let refund_money = app.checkEmpty(info.refund_money, '0.00');
          let refund_balance = app.checkEmpty(info.refund_balance, '0.00');
          let freight = app.checkEmpty(info.freight, '0.00'); // 运费

          let require_array = that.data.require_array;

          if (detail != '' && detail.is_virtual == 0 && detail.shipping_status != 3 && detail.shipping_status > 0) {
            require_array[1] = {
              id: 2,
              value: '我要退款，并且退货'
            };
          }

          let reason_array = that.data.reason_array;
          let reason_index = 0;
          if (detail != '') {
            detail.refund_status = app.checkEmpty(detail.refund_status, 0);
            if (detail.refund_status == 5) {
              detail.refund_time = app.checkEmpty(detail.refund_time, 0) > 0 ? time.formatTime(detail.refund_time, 'Y-M-D h:m:s') : '';
            }
          }
          if (detail != '' && app.checkEmpty(detail.refund_reason, '') != '' && detail.refund_status == -3) {
            reason_index = -1;
            for (let index in reason_array) {
              if (detail.refund_reason == reason_array[index].value) {
                reason_index = index;
              }
            }
            reason_index = reason_index == -1 ? reason_array.length - 1 : reason_index;
          }

          that.setData({
            detail: detail,
            refund_money: refund_money,
            refund_balance: refund_balance,
            freight: freight,
            detail: detail,
            require_array: require_array,
            reason_index: reason_index,
            is_load: 1
          });
        }
      }
    });
  },

  /**
   * 输入退款金额
   */
  inputRefundRequireMoney: function (e) {
    let refund_require_money = e.detail.value;
    this.setData({
      refund_require_money: refund_require_money
    })
  },

  /**
   * 输入物流公司
   */
  inputExpressCompany: function (e) {
    let refund_express_company = e.detail.value;
    this.setData({
      refund_express_company: refund_express_company
    })
  },

  /**
   * 输入运单号
   */
  inputShippingNo: function (e) {
    let refund_shipping_no = e.detail.value;
    this.setData({
      refund_shipping_no: refund_shipping_no
    })
  },

  selector: function (event) {
    let that = this;

    let require_index = that.data.require_index;
    let reason_array = that.data.reason_array;
    let reason_index = that.data.reason_index;
    let select_type = event.currentTarget.dataset.type;
    let index = event.detail.value;

    if (select_type == 1) {
      require_index = index;
    } else {
      reason_index = index;
    }

    that.setData({
      require_index: require_index,
      reason_index: reason_index,
    });
  },

  /**
   * 退款说明
   */
  refundReason: function (event) {
    let that = this;

    let refund_reason = event.detail.value;

    that.setData({
      refund_reason: refund_reason
    })
  },

  /**
   * 退款/退货申请 售后
   */
  refund: function (event) {
    let that = this;
    let order_id = that.data.detail.order_id;
    let order_goods_id = that.data.order_goods_id;
    let refund_require_money = parseFloat(that.data.refund_require_money);
    let refund_balance = parseFloat(that.data.refund_balance);
    let refund_money = that.data.refund_money;
    let require_array = that.data.require_array;
    let reason_array = that.data.reason_array;
    let require_index = that.data.require_index;
    let reason_index = that.data.reason_index;
    let refund_reason = that.data.refund_reason;
    let refund_type = require_array[require_index].id;
    let refundFlag = that.data.refundFlag;

    if (refundFlag == 1) {
      return false;
    }
    app.clicked(that, 'refundFlag');

    if (refund_require_money == '' && parseFloat(refund_money > 0)) {
      app.showBox(that, '请输入退款金额');
      app.restStatus(that, 'refundFlag');
      return false;
    }

    if (parseFloat(refund_money) > 0 && isNaN(parseFloat(refund_require_money))) {
      app.showBox(that, '请输入正确的退款金额');
      app.restStatus(that, 'refundFlag');
      return false;
    }

    if (parseFloat(refund_require_money) > parseFloat(refund_money)) {
      app.showBox(that, '超出可退金额范围');
      app.restStatus(that, 'refundFlag');
      return false;
    }

    if (reason_index != 4) {
      refund_reason = reason_array[reason_index].id;
    } else {
      if (refund_reason == '') {
        app.showBox(that, '请输入退款说明');
        app.restStatus(that, 'refundFlag');
        return false;
      }
    }

    app.sendRequest({
      url: 'System.Order.applyOrderRefund',
      data: {
        order_id: order_id,
        order_goods_id: order_goods_id,
        refund_type: refund_type,
        refund_require_money: refund_require_money,
        refund_reason: refund_reason,
      },
      success: function (res) {
        let code = res.code;
        let data = res.data;
        if (code == 0) {
          if (data > 0) {
            app.showBox(that, '操作成功');
            that.getRefundInfo();
            app.restStatus(that, 'refundFlag');
          } else {
            app.showBox(that, '操作失败');
            app.restStatus(that, 'refundFlag');
          }
        }
      }
    });
  },

  /**
   * 买家退货 售后
   */
  returnsGoods: function () {
    let that = this;
    let id = that.data.detail.id;
    let order_id = that.data.detail.order_id;
    let order_goods_id = that.data.order_goods_id;
    let refund_express_company = that.data.refund_express_company;
    let refund_shipping_no = that.data.refund_shipping_no;
    let returnsGoodsFlag = that.data.returnsGoodsFlag;

    if (returnsGoodsFlag == 1) {
      return false;
    }
    app.clicked(that, 'returnsGoodsFlag');

    if (refund_express_company == '') {
      app.showBox(that, '请输入物流公司');
      app.restStatus(that, 'returnsGoodsFlag');
      return false;
    }

    if (refund_shipping_no == '') {
      app.showBox(that, '请输入快递单号');
      app.restStatus(that, 'returnsGoodsFlag');
      return false;
    }

    app.sendRequest({
      url: 'System.Order.orderRefund',
      data: {
        order_id: order_id,
        order_goods_id: order_goods_id,
        refund_express_company: refund_express_company,
        refund_shipping_no: refund_shipping_no,
      },
      success: function (res) {
        let code = res.code;
        let data = res.data;
        if (code == 0) {
          if (data > 0) {
            app.showBox(that, '操作成功');
            setTimeout(function () {
              wx.switchTab({
                url: '/pages/member/member/member',
              })
            }, 1500)

          } else {
            app.showBox(that, '操作成功失败');
            app.restStatus(that, 'returnsGoodsFlag');
          }
        }
      }
    });
  }

})